using System;
using System.Xml.Linq;
using System.Linq;
using System.Collections.Generic;
//using System.Xml.Serialization;
using System.IO;

namespace WebService
{
	public class LoadXml
	{
		private List<XElement> m_listeMusiques;
		
		public LoadXml (string path) {			
			
			try {
				XDocument objXmlDoc = XDocument.Load(@"" + path);
				m_listeMusiques = (from xml in objXmlDoc.Descendants("Musics").Descendants("File")
				                    	where xml.Attribute("Selected").Value.Equals("true")
										select xml).ToList();
			
			}
			catch {
				m_listeMusiques = new List<XElement>();
			}
		}
		
		public List<string[]> GetMusiques () {
			List<string[]> musiques = new List<string[]>();
			
						
			foreach(XElement musique in m_listeMusiques) {
				string[] tmp = new string[2];
				tmp[0] = musique.Attribute("Name").Value;
				tmp[1] = musique.Attribute("Key").Value;
				musiques.Add(tmp);
			}
			
			return musiques;
		}
		
		public byte[] GetMusique(string keyMusic) {
			string path = m_listeMusiques.Find((XElement doc)=> doc.Attribute("Key").Value == keyMusic).Attribute("Path").Value;
			
			System.IO.FileStream fs = null;
			fs = System.IO.File.Open(path, FileMode.Open,FileAccess.Read);
			
			byte[] musique = new byte[fs.Length];
			
			fs.Read(musique, 0, (int)fs.Length);
			fs.Close();
			
			return musique; 
		}
	}
}

